<?php
declare (strict_types = 1);

namespace app\event;

use app\admin\model\ProjectFeeitem;
use think\facade\Log;

class WfNotify
{

    public function handle($data){
        $data = json_decode(json_encode($data), true);
        $flow_model = $data['flow_model'];
        $extend = $data['extend'];
        $param = [
            'audit_state' => $data['state'],
            'wokerflow_id' => $data['id']
        ];
        $model_name = ['','Reimbursement', 'Advance', 'Payment'][$flow_model];
        $extend_model = new \ReflectionClass("app\\common\\model\\apply\\".$model_name);
        $extend_model_class = $extend_model->newInstance();
        $extend_model_class->where([
            ['no', '=', $extend]
        ])->update($param);
        // 审核通过
        if($data['state'] == 3){
            $flow_data = $extend_model_class->where([
                ['no', '=', $extend]
            ])->find();
            $projectFeeitemModel = ProjectFeeitem::where([
                ['project_id', '=', $flow_data['project_id']],
                ['feeitem_id', '=', $flow_data['feeitem_id']]
            ])->findOrEmpty();
            if(!$projectFeeitemModel->isEmpty()){

                $amount = $flow_model == 3 ? $data['content']['pay_amount'] : $data['content']['amount'];
                $projectFeeitemModel->calculate = bcadd($projectFeeitemModel->calculate, (string)$amount);
                $projectFeeitemModel->save();
            }
        }
    }
}
